package com.saint.swipemenulistview.activity;


import android.app.Activity;
import android.content.Intent;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.saint.swipemenulistview.R;
import com.saint.swipemenulistview.bean.BaseBean;
import com.saint.swipemenulistview.bean.LoginBean;
import com.saint.swipemenulistview.http.HttpCallBack;
import com.saint.swipemenulistview.http.HttpUtil;

import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2017/11/3.
 */

public class HttpTestActivity extends BaseActivity {
    @ViewInject(R.id.call_data_tv)
    private TextView dataTv;
    @ViewInject(R.id.user_name_et)
    private EditText userName_et;
    @ViewInject(R.id.pwd_et)
    private EditText pwd_et;

    private String userName, pwd;

    @Override
    protected int setContent() {
        return R.layout.act_http_test;
    }


    @Override
    protected void initView() {
        dataTv.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    @Override
    protected void initDate() {

    }

    @Override
    protected void startAct(Activity startAct) {
        intent = new Intent(startAct, HttpTestActivity.class);
        startAct.startActivity(intent);
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.post_data_btn:
                getLoginData();
                break;

            default:
                break;
        }
    }

    private void getLoginData() {
//        Map<String, String> params = new HashMap<>();
        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        String szImei = TelephonyMgr.getDeviceId();
        if (szImei == null) {
            szImei = Settings.Secure.getString(act.getContentResolver(),
                    Settings.Secure.ANDROID_ID);
        }
        userName = userName_et.getText().toString();// 注册手机号
        pwd = pwd_et.getText().toString();// 注册密码
        HashMap<String, String> map = new HashMap<>();
        map.put("user_mobile", userName);
        map.put("imei", szImei);
        map.put("user_flag", "3");
        //坐标
        map.put("lat", "0");
        map.put("lng", "0");
        // tempGuid为登录后服务器生成的本次session 唯一id，用于防止多方登录，没登录时此id为""
        map.put("token", "");
        map.put("jgid", "");
        map.put("user_pwd", pwd);
        map.put("method", "kkpb.user.login");
        HashMap<String, String> params = HttpUtil.getParamMap(map);
        Log.e("HttpTest: ", params.toString());
        Call<BaseBean<String>> call = HttpUtil.getInstance().requestData(params);
        call.enqueue(new HttpCallBack<BaseBean<String>>() {
            @Override
            public void onSuc(Response<BaseBean<String>> response) {
            }

            @Override
            public void onFail(String message) {

            }

            @Override
            public void onAutoLogin() {

            }
        });
//        Call<String> call = HttpUtil.getInstance().requestData(params);
//        call.enqueue(new Callback<String>() {
//            @Override
//            public void onResponse(Call<String> call, Response<String> response) {
//                if (response.isSuccessful()) {
//                    LoginBean bean = new Gson().fromJson(response.body(), LoginBean.class);
//                    if (200 == bean.getCode()) {
//                        dataTv.setText(bean.getData().toString());
//                    } else {
//                        dataTv.setText("Code: " + bean.getCode() + " Message: " + bean.getMessage());
//                    }
//                } else {
//                    dataTv.setText("Code: " + response.code() + "  Message: " + response.message());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<String> call, Throwable t) {
//                dataTv.setText(t.getMessage());
//            }
//        });
    }


}
